home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: gail.ripco.com!mambuhl
- From: mambuhl@ripco.com (Martin Ambuhl)
- Subject: Re: Problem with program
- X-Nntp-Posting-Host: golden.ripco.com
- Message-ID: <DLHGws.3q9@rci.ripco.com>
- Sender: usenet@rci.ripco.com (Net News Admin)
- Organization: Ripco Internet BBS Chicago
- Date: Sat, 20 Jan 1996 14:14:51 GMT
- X-Ident-Sender: mambuhl
-
- elvemo@sn.no (Rune Elvemo)
- in <4dkr5u$ro7@hasle.sn.no> asks:
-
- >Anyone who can figure out what is wrong with this program?
-
- Logically, there are lots of problems. The obvious syntax errors are fixed
- below.
-
- >I tried to compile it, but my compiler wouldn't.....
-
- Of course not.
-
- >Ex: It said that the prototype needed a semicolon, but it
- >don't.........
-
- Well, you have to know where to look. In this case, the declaration of the
- struct Text needs a semicolon. My changes to your code are marked with
- "/* mha ... */" comments.
-
-
- /* Loadfile.c - loads a txt file, and shows it */
-
- #include <stdio.h>
- #include <stdlib.h> /* mha - added */
-
- struct Text {
- char *str;
- struct Text *next;
- }; /* mha - added ';' */
-
- typedef enum {
- TRUE, FALSE
- } BOOL; /* mha - was "enum BOOL {TRUE,FALSE};" */
-
- /* prototype */
- void PText(struct Text *);
-
- int /* mha - added return type */ main(int argc, char *argv[])
- {
- BOOL done = FALSE;
- FILE *txtfile;
- struct Text *first, *tst;
-
- if (argc > 1) {
- if ((txtfile = fopen(argv[1], "r"))) { /* mha - added () */
- if ((first = malloc(sizeof(struct Text)))) {
- /* mha - added (). Replaced "calloc(1, sizeof(struct
- * Text))". Usually calloc is wasted motion. */
- tst = first;
- if (fgetc(txtfile) != EOF) {
- /* mha - This did have a test with -1, a very bad
- * practice. Notice that this code loses the
- * character obtained. Is this what you want? */
- while (!done) {
- if ((tst->str = malloc(256))) {
- /* mha - added (). Replaced "calloc(256,
- * sizeof(char))". Not only is calloc
- * wasted motion, but also sizeof(char) is
- * just typing practice. The hardcoded 256
- * is still a problem */
- do {
- *tst->str = fgetc(txtfile);
- tst->str++;
- *tst->str = 0;
- /* check if the last char was LineFeed
- * or EOF */
-
- } while ((*(tst->str - 1) != EOF) /* mha - was
- non-standard -1 */ &&
- (*(tst->str) != '\n') /* mha - was
- non-standard 10 */ );
-
- if (*tst->str == EOF) /* mha - was
- * non-standard -1 */
- done = TRUE;
- else {
- if ((tst->next = malloc(sizeof(struct Text)))) {
- /* mha - added () - was "calloc(1,
- * sizeof(struct Text))" */
- tst = tst->next;
-
- } else {
- done = TRUE;
- printf("\n**!!Not enough memory!!**\n");
- /* mha - stderr exists for a
- * reason. Try using it */
- }
- }
- }
- }
- tst->next = malloc(sizeof(struct Text));
- /* mha - was "calloc(1, sizeof(struct Text))".
- * calloc() is the wrong way to make sure that
- * pointers in the allocated struct are set to
- * NULL. Do it yourself. No extra points for
- * being lazy */
- PText(first);
- }
- } else
- printf("\n**!!Not enough memory!!**\n");
- }
- }
- return 0; /* mha - added explicit return */
- }
-
- /* Print the text that we have got */
- void PText(struct Text * txt)
- {
- BOOL done = FALSE;
- struct Text *jump;
- jump = txt;
- while (!done) {
- printf("%s", jump->str);
- while (txt->next != NULL)
- /* mha - was "while (*txt->next != 0)" */ {
- jump = jump->next;
- printf("%s\n", jump->str);
- /* mha - was 'printf("%s\n", jump);'. No specification for
- * printf ever promised that "%s" was appropriate for a
- * "struct Text *" */
- }
- }
- }
-
-
- --
- * Martin Ambuhl net: mambuhl@ripco.com
- * Chicago, IL (USA)
-